package com.uit.project.bustrip;

import com.uit.project.bustrip.appconfig.AppsConfig;
import com.uit.project.bustrip.bo.CustomerBOImpl;
import com.uit.project.bustrip.bointerface.CustomerBO;
import com.uit.project.bustrip.entity.Customer;
import com.uit.project.bustrip.valueobject.JSONResult;

import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ActiveCustomer extends Activity {

	private String phone = "";
	private String code = "";
	private TextView message;
	private EditText txtCode;
	private Button btnActive;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_active_customer);
		initView();
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			phone = extras.getString("phone");
			code = extras.getString("code");
			String msg = "Please input this code " + code + " to active";
			message.setText(msg);
		}
	}
	private void initView() {
		message = (TextView) findViewById(R.id.txtmessage);
		txtCode = (EditText) findViewById(R.id.code);
		btnActive = (Button) findViewById(R.id.btnactive);
		btnActive.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
				.permitAll().build();
				StrictMode.setThreadPolicy(policy);
				CustomerBO customerBO = new CustomerBOImpl(getApplicationContext());
				String strCode = txtCode.getText().toString();
				JSONResult jsonResult = customerBO.activeCustomerAsync(phone, strCode);
				afterActiveCustomer(jsonResult);
			}
		});
	}
	private void afterActiveCustomer(JSONResult jsonResult){
		if(jsonResult == null){
			Toast.makeText(getApplicationContext(), AppsConfig.MESSAGE_CONNECTION_ERROR, Toast.LENGTH_SHORT).show();
			return;
		}
		if(jsonResult.getResult().equals(AppsConfig.RESULT_SUCCESS)){
			Customer customer = (Customer) jsonResult.getData();
			CustomerBO customerBO = new CustomerBOImpl(getApplicationContext());
			long rs = customerBO.insert(customer);
			Intent i = new Intent(getApplicationContext(),Home.class);
			startActivity(i);
		}
		else{
			Toast.makeText(getApplicationContext(), jsonResult.getMessage(), Toast.LENGTH_SHORT).show();
			String msg = "Please input this code " + jsonResult.getData().toString() + " to active";
			message.setText(msg);
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.active_customer, menu);
		return true;
	}

}

